package com.gzz.orchid.common.core.tuple;


import com.gzz.orchid.common.core.exception.ArgumentException;
import com.gzz.orchid.common.core.util.I18nUtil;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Objects;

/**
 * 折扣率 primitive
 *
 * @author higmd
 */
public class DiscountRate {
    private final Integer val;
    // 构造函数
    public DiscountRate(int value) {
        if (value < 0 || value > 99) {
            throw new ArgumentException(I18nUtil.ofMessage("无效的折扣，折扣值必须在【0 ~ 99】之间"));
        }
        this.val = value == 0 ? 100 : value;
    }

    // 构造函数
    public Integer getValue() {
        return this.val;
    }

    /**
     * 计算扣后价钱
     *
     * @param amount 金额
     */
    public BigDecimal calcPrice(BigDecimal amount) {
        if (Objects.isNull(amount)) {
            throw new ArgumentException(I18nUtil.ofMessage("计价金额不能空值"));
        }
        if (amount.compareTo(BigDecimal.ZERO) == 0) {
            return amount;
        }
        return amount
                .multiply(new BigDecimal(this.val))
                .divide(new BigDecimal(100), RoundingMode.HALF_UP);
    }
}
